package com.kiln.juxingqs.ui

import android.content.Intent
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.ZrSpecialDetailAdapter
import com.kiln.juxingqs.api.bean.ZrOrder
import com.kiln.juxingqs.databinding.ActivityZrSpecialDetailBinding
import com.kiln.juxingqs.ui.vm.ZrPickOrderViewModel




/**
 * 特价券 - 明细(资产明细)
 */
class ZrSpecialDetailActivity :
    BaseMiddleActivity<ZrPickOrderViewModel, ActivityZrSpecialDetailBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_zr_special_detail
    }

    var goods_id: String? = ""
    var goods_price: String? = ""
    var goods_name: String? = ""
    var num: String? = ""
//    var titleName: String? = ""

    val adapter: ZrSpecialDetailAdapter by lazy {
        ZrSpecialDetailAdapter(this)
    }

    override fun initViewModel(): ZrPickOrderViewModel = createViewModel()

    override fun initView() {
        binding?.apply {
            event = this@ZrSpecialDetailActivity
            vm = viewModel

            goods_id = intent.getStringExtra("goods_id")
            goods_price = intent.getStringExtra("goods_price")
//            titleName = intent.getStringExtra("titleName")

            goods_name = intent.getStringExtra("goods_name")
            num = intent.getStringExtra("num")

        }

        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.adapter = adapter

        binding.refreshLayout.setOnRefreshListener {
            viewModel.pageOne.value = 0
            adapter.hasMoreState.value = true
            viewModel.holdSpecialPrice(goods_id, "0")
        }

        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdSpecialPrice(goods_id, "0")
            }
        }

        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdSpecialPrice(goods_id, "0")
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })

        viewModel.callSpecialPrices.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.order_list as MutableList<ZrOrder.Order?>
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

            binding.tvColumn1.layoutParams = LinearLayout.LayoutParams(
                0,
                LinearLayout.LayoutParams.MATCH_PARENT,
                1f
            )
        binding. tvColumn2.gravity=Gravity.CENTER
        binding. tvColumn4.isVisible = false
        binding.  tvColumn5.isVisible = true


    }

    override fun initData() {
        viewModel.pageOne.value = 0
        viewModel.holdSpecialPrice(goods_id, "0")
    }


    //摘牌特价商品
    fun goSpecialGood() {
        startActivity(Intent(this, ZrPickOrderListActivity::class.java).apply {
            putExtra("goods_id", "${goods_id}")
            putExtra("goods_price", "${goods_price}")
            putExtra("r_type", "1")
            putExtra("d_type", "2")
            putExtra("u_type", "1")
            putExtra("titleName", "摘牌特价商品")
        })
    }

    fun detail() {
        startActivity(Intent(this, XyzrNewActivity::class.java).apply {
            putExtra("detail_type", "22")
            putExtra("goods_id", goods_id)//新goods_id
        })
    }

}